home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Palettes / TTools / TToolsPalette / Utilities.subproj / SortedStorage.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.7 KB  |  122 lines

  1. /* SortedStorage.m
  2.  * Written By:  Thomas Burkholder
  3.  *
  4.  * You may freely copy, distribute, and reuse the code in this example.
  5.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  6.  * fitness for any particular use.
  7.  */
  8.  
  9. #import "SortedStorage.h"
  10.  
  11. @implementation SortedStorage:Storage
  12.  
  13. - initCount:(unsigned)count elementSize:(unsigned)sizeInBytes description:(const char *)descriptor
  14. {
  15.     [super initCount:count elementSize:sizeInBytes description:descriptor];
  16.     agent = nil;
  17.     return self;
  18. }
  19.  
  20. - setAgent:anObject
  21. {
  22.     agent = anObject;
  23.     return self;
  24. }
  25.  
  26. - agent
  27. {
  28.     return agent;
  29. }
  30.  
  31. - (id)subdirectoryForElementAt:(int)at
  32. {
  33.     return [agent subdirectoryFor:[self elementAt:at] sender:self];
  34. }
  35.  
  36. - (BOOL)isLeafAt:(int)at
  37. {
  38.     return [agent isLeaf:[self elementAt:at] sender:self];
  39. }
  40.  
  41. - (const char *)displayStringForElementAt:(int)at
  42. {
  43.     return [agent displayStringFor:[self elementAt:at] sender:self];
  44. }
  45.  
  46. - (int)compareElementAt:(int)at with:(void *)anElement
  47. {
  48.     return [agent compare:[self elementAt:at] with:anElement sender:self];
  49. }
  50.  
  51. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  52. {
  53.     id c;
  54.     int i,n;
  55.     id candidate = self;
  56.  
  57.     if (column != 0) {
  58.         for(i=0;(i<column);i++) {
  59.             candidate = [candidate subdirectoryForElementAt:[[sender matrixInColumn:i] selectedRow]];
  60.             if ((!candidate) || (![candidate isKindOf:[SortedStorage class]])) return 0;
  61.         }
  62.     }
  63.  
  64.     for(i=0,n=[candidate count];i<n;i++) {
  65.         [matrix addRow];
  66.         c = [matrix cellAt:i :0];
  67.         [c setLeaf:[candidate isLeafAt:i]];
  68.         [c setLoaded:YES];
  69.         [c setStringValue:[candidate displayStringForElementAt:i]];
  70.     }
  71.     return i;
  72. }
  73.  
  74. - (const char *)browser:sender titleOfColumn:(int)column
  75. {
  76.     return [agent titleOfColumn:column];
  77. }
  78.  
  79. - binaryInsert:(void *)anElement between:(int)lower and:(int)upper
  80. {
  81.     int r,c = lower + (1+upper-lower)/2;
  82.  
  83.     if (lower == upper) {  
  84.         if ([self compareElementAt:lower with:anElement]>0) //insert before
  85.             return [self insertElement:anElement at:lower];
  86.         else  // insert after
  87.             return [self insertElement:anElement at:lower+1];
  88.     }
  89.  
  90.     r = [self compareElementAt:c with:anElement];
  91.     if (r>0)  // Recursion is Good.
  92.         return [self binaryInsert:anElement between:lower and:c-1];
  93.     else if (r<0)  // In fact, Recursion is Holy.
  94.         return [self binaryInsert:anElement between:c and:upper];
  95.     else  // if equal insert right now.
  96.         return [self insertElement:anElement at:c];
  97. }
  98.  
  99. - addElement:(void *)anElement
  100. {
  101.     // Binary insertion...
  102.     if (![self count])
  103.         return [super addElement:anElement];
  104.     [self binaryInsert:anElement between:0 and:[self count]-1];
  105.     return self;
  106. }
  107.  
  108. - read:(NXTypedStream *)stream
  109. {
  110.     [super read:stream];
  111.     agent = NXReadObject(stream);
  112.     return self;
  113. }
  114.  
  115. - write:(NXTypedStream *)stream
  116. {
  117.     [super write:stream];
  118.     NXWriteObject(stream,agent);
  119.     return self;
  120. }
  121.  
  122. @end